package com.sybase.dao;

/*
	Author: Leo
	Creation Date: 27-7-2012
	Creation Time: 12:02 AM
*/

import java.io.*;
import java.util.*;
import java.sql.*;

import com.customds.impl.CocoaMap;
import com.sybase.gui.StockFrame;
import com.sybase.gui.CocoaTable;

public class Caller {
	// Data member declaration
	/*
	private static final String DATABASE_QUERY = "styx..test_styx1";
	private static final String THREAD_NAME = "@comment";
	private static final Object THREAD_VALUE = "Placeholder";
	*/
	
	Connection connection;
	String dbQuery;
	String threadName;
	Object threadValue;
	// Statement stmt;
	CallableStatement cstmt;
	ResultSet rs;
	DBConnect dbConnect;
	GenericDAO dao;
	StoredProcedureParameters spParameters;
	CocoaMap cocoaMap;
	StockFrame stockFrame;
	CocoaTable cocoaTable;
	
	Map<String, StoredProcedureParameters> inputParameters;
	// Map<String, StoredProcedureParameters> outputParameters;
	
	String columnName;
	
	// Constructor
	public Caller() {
		connection = null;
		dbQuery = "";
		threadName = "";
		threadValue = null;
		// stmt = null;
		cstmt = null;
		rs = null;
		dbConnect = null;
		dao = null;
		spParameters = null;
		cocoaMap = null;
		stockFrame = null;
		cocoaTable = null;
		
		inputParameters = new LinkedHashMap<String, StoredProcedureParameters>();
		// outputParameters = new LinkedHashMap<String, StoredProcedureParameters>();
		
		columnName = "";
	} // End of constructor

	public static void main(String[] args) throws IOException, SQLException {
		// Shenanigans
		System.out.println("------------------------");
		System.out.println("Sybase Connection Layer");
		System.out.println("------------------------");
		// System.out.print("\n");
		
		Caller caller = new Caller();
		caller.input();
		caller.establishConnection();
	} // End of main
	
	private void input() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		char ch;
		int z;
		
		System.out.print("Procedure name: ");
		dbQuery = br.readLine();
		System.out.print("Any input parameters? ");
		ch = (br.readLine()).charAt(0);
		
		while(ch == 'Y' || ch == 'y') {
			System.out.print("Parameter name: ");
			threadName = br.readLine();
			System.out.println("Pick a type...");
			System.out.println("1)Integer\t2)Varchar");
			z = Integer.parseInt(br.readLine());
			System.out.print("Parameter value: ");
			
			switch(z) {
				case 1: threadValue = Integer.parseInt(br.readLine());
						inputParameters.put(threadName, new StoredProcedureParameters(threadName, threadValue, Types.INTEGER));
						break;
				case 2: threadValue = br.readLine();
						inputParameters.put(threadName, new StoredProcedureParameters(threadName, threadValue, Types.VARCHAR));
						break;
				default:System.out.print("\n");
						System.out.println("[echo] Invalid choice!");
			} // End of switch-case block
			
			System.out.print("\n");
			System.out.print("More input parameters (Y/N)? ");
			ch = (br.readLine()).charAt(0);
		} // End of while loop
		
		/*
		System.out.print("Column name: ");
		columnName = br.readLine();
		*/
		
	} // End of method
	
	private void establishConnection() throws SQLException {
		dbConnect = new DBConnect();
		dao = new GenericDAO();
		cocoaMap = new CocoaMap();
		connection = dbConnect.connectToSybase();
		ArrayList<String> headerRow = new ArrayList<String>();
		ArrayList<String> cocoaRow = new ArrayList<String>();
		int tableSize;
		int i; // Counter variable
		stockFrame = new StockFrame();
		cocoaTable = new CocoaTable();
		
		// For debugging purpose
		System.out.print("\n");
		for(Map.Entry<String, StoredProcedureParameters> entry : inputParameters.entrySet()) {
			spParameters = entry.getValue();
			
			System.out.println("[echo:iMap:" + entry.getKey() + "] name: " + spParameters.getParameterName()
					+ ", value: " + spParameters.getParameterValue() + ", type: " + spParameters.getSqlDataType());
		}
		
		rs = dao.executeSimpleProcedure(connection, dbQuery, inputParameters, null, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		
		// Legacy code
		/*
		// stmt = connection.createStatement();
		cstmt = connection.prepareCall(DATABASE_QUERY);
		rs = cstmt.executeQuery();
		*/
		
		// Legacy code
		/*
		while(rs.next()) {
			System.out.println(rs.getString(1) + "    " + rs.getString(2));
		}
		*/
		
		cocoaMap.setKey(dbQuery);
		cocoaMap.setResultSet(rs);
		cocoaMap.initComponents();
		tableSize = cocoaMap.getTableSize();
		
		// For debugging purpose
		System.out.println("[echo:TABLE_SIZE] " + tableSize);
		
		System.out.print("\n");
		System.out.println("Output");
		System.out.println("------");
		System.out.println("[echo] All results redirected to Stock Frame (COCOA)");
		
		// Disabled temporarily
		// System.out.println("nm_game\ttype_media");
		
		// Configure GUI environment
		cocoaTable.setRowSize(tableSize);
		cocoaTable.setColumnSize(9);
		cocoaTable.defineTableData();
		headerRow.add("au_id");
		headerRow.add("au_lname");
		headerRow.add("au_fname");
		headerRow.add("phone");
		headerRow.add("address");
		headerRow.add("city");
		headerRow.add("state");
		headerRow.add("country");
		headerRow.add("postalcode");
		
		cocoaTable.setHeaderRow(headerRow);
		cocoaTable.setHeaderData();
		
		// Formerly used to Print output
		// Now some other purpose
		for(i = 0; i < tableSize; i++) {
			// Disabled temporarily
			/*
			cocoaRow = cocoaMap.getCocoaRow("nm_game", "type_media");
			System.out.println(cocoaRow.get(0) + "\t" + cocoaRow.get(1));
			*/
			
			cocoaRow = cocoaMap.getCocoaRow("au_id", "au_lname", "au_fname", "phone", "address", "city", "state", "country", "postalcode");
			cocoaTable.setCocoaRow(cocoaRow);
			cocoaTable.setTableData();
		}
		
		cocoaTable.initComponents();
		stockFrame.setTable(cocoaTable.getTable());
		stockFrame.setupGUI();
	} // End of method
	
} // End of class